package com.wms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wms.entity.OperationLogAnnotation;
import com.wms.bean.PageBean;
import com.wms.code.CodeFactory;
import com.wms.dto.AllocatedetailAuditDTO;
import com.wms.dto.AllocatedetailAuditVo;
import com.wms.dto.TransferItemAuditDTO;
import com.wms.dto.TransferPassDTO;
import com.wms.entity.*;
import com.wms.mapper.AllocateInMapper;
import com.wms.mapper.AllocateMapper;
import com.wms.mapper.AllocatedetaiAuditMapper;
import com.wms.service.AllocatedetaiAuditService;
import com.wms.service.StockInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static cn.hutool.core.date.DateUtil.now;

@Service
public class AllocatedetaiAuditServiceImpl implements AllocatedetaiAuditService {
    @Autowired
    AllocatedetaiAuditMapper allocatedetaiAuditMapper;

    @Autowired
    AllocateMapper allocateMapper;

    @Autowired
    StockInService stockInService;

    @Autowired
    AllocateInMapper allocateInMapper;


    String prefix = "RK"; // 可以更改为不同的前缀

    String prefix1 = "TS"; // 可以更改为不同的前缀
    String prefix2 = "PC"; // 可以更改为不同的前缀
    @Override
    public PageBean<List<AllocateDetailAudit>> query(AllocatedetailAuditDTO allocatedetailAuditDTO) {
        //分页处理
        PageHelper.startPage(allocatedetailAuditDTO.getPage(),allocatedetailAuditDTO.getPageSize());
        //条件查询
        List<AllocateDetailAudit> list = allocatedetaiAuditMapper.selectByOrderId(allocatedetailAuditDTO);
        PageInfo<AllocateDetailAudit> pageInfo = new PageInfo<>(list);
        return new PageBean<List<AllocateDetailAudit>>(allocatedetailAuditDTO.getPage(),allocatedetailAuditDTO.getPageSize(),pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 审核业务
     * @param allocatedetailAuditVo
     * @return
     */
    @Override
    @Transactional
    @OperationLogAnnotation(type = "审核", description = "调拨审核操作")
    public int update(AllocatedetailAuditVo allocatedetailAuditVo) {
        int results =0;
        //判断是否通过审核 审核状态为2 表示审核通过
        if(allocatedetailAuditVo.getAuditStatus()==2){
            // 审核通过后更改调拨单状态和备注信息
            int n = allocateMapper.updateAuditStatus(allocatedetailAuditVo.getAuditStatus(),allocatedetailAuditVo.getNote(),allocatedetailAuditVo.getOrderId());
            //前端传递审批数量，调拨单详情保存审批数量 详情是一对多 采用批量修改
            List<TransferItemAuditDTO> list=allocatedetailAuditVo.getItemAuditDTOS();
            list.forEach(el->{
                allocatedetaiAuditMapper.updateAuditNum(el.getId(),el.getAuditNum());
            });
            //以上是审核通过后调拨单和调拨明细的修改
            System.out.println("-------------------------调拨出库操作-------------------");

            System.out.println("-------------------------出库以后对调拨入库操作-------------");
            //查询出所有的需要加入到调拨入库的调拨单和调拨明细 ----------重要一步
            Allocate allocate = allocateMapper.selectAllocateWithDetails(allocatedetailAuditVo.getOrderId());
            //计算要添加到调拨入库中的商品总数
            int sum = 0;
            List<TransferItemAuditDTO> itemAuditDTOS = allocatedetailAuditVo.getItemAuditDTOS();
            for(int i=0;i<itemAuditDTOS.size();i++){
                //计算调拨入库总数
                sum+=itemAuditDTOS.get(i).getAuditNum();
            }
            //将查询出来的调拨单和调拨单明细转化为调拨入库用来增加信息
            String MaxAllocateInCode =  allocateInMapper.selectMaxCode();
            //查询最大的TS调出单号
            String MaxTSAllocateInCode = allocateInMapper.selectMaxTSCode();
            //使用创建者模式对allocateIn赋值
            //仓库id不作赋值，知道即可
            AllocateIn allocateIn = AllocateIn.builder()
                    .allocateInCode(CodeFactory.createArrivalCode(MaxAllocateInCode,prefix))
                    .tsallocateInCode(CodeFactory.createArrivalCode(MaxTSAllocateInCode,prefix1))
                    .inName(allocate.getInName())
                    .outName(allocate.getOutName())
                    //发送时间为当前时间  入库时间操作收获后变动
                    .sendTime(now())
                    //温区写死
                    .warmzone("冷冻")
                    //商品总数 调拨明细中审核通过的商品综合
                    .num(sum)
                    .allocateName("张开心")
                    .build();
            //保存调拨入库
            int a =allocateInMapper.insertAllocateIn(allocateIn);
            //保存调拨入库明细 批量保存
            List<Allocatedetail> allocatedetails =  allocate.getAllocatedetails();
            allocatedetails.forEach(el->{
              String MaxPCCode=  allocateInMapper.selectMaxPCCode();
            AllocateInDetail allocateInDetail = AllocateInDetail.builder()
                    .batch(CodeFactory.createArrivalCode(MaxPCCode,prefix2))
                    .category("食品")
                    .productName(el.getName())
                    .spec("无")
                    .num(el.getAuditNum())
                    .unit("货")
                    .productCode(el.getCode())
                    .freight("B01-01-待处理（虚假数据）")
                    .instoreId(allocatedetailAuditVo.getOrderId())
                    .build();
                //批量进行调入详情创建
                allocateInMapper.insertAllocateInDetail(allocateInDetail);
            });
            results=1;
        }else{
            //审核不通过 直接返回审核0 后续控制层直接根据结果返回审核失败！
            results=0;
        }
        return results;
    }


    @Override
    public List<TransferPassDTO> selectPass(Integer orderId) {
        return allocatedetaiAuditMapper.selectPass(orderId);
    }
}
